package com.sise.thread.Lock;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 作者：idea
 * 日期：2018/6/24
 * 描述：
 */
public class TestLock {

    public List<Integer> list=new ArrayList<>();
    public Lock lock=new ReentrantLock();
    public void insert(){

        lock.lock();
        try {
            System.out.println("开始加锁！");
            for(int i=0;i<100;i++){
                Thread.sleep(10);
                list.add(i);
            }
            System.out.println(list.size());
        }catch (Exception e){

        }finally {
            System.out.println("开始释放锁！");
            lock.unlock();
        }
    }


    public static void main(String[] args) {
        final TestLock testLock=new TestLock();
        new Thread(){
            public void run(){
                testLock.insert();
            }
        }.start();
        new Thread(){
            public void run(){
                testLock.insert();
            }
        }.start();


    }
}
